<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        a {
            text-decoration: none;
            color: #000000;
        }
        
        .login-bg {
            /* 灰色背景层 */
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0px;
            left: 0px;
            background-color: rgba(0, 0, 0, .3);
            z-index: -1;
            /* 降级一层 */
            display: none;
        }
        
        .login-header a {
            width: 200px;
            font-size: 20px;
            font-weight: 600;
            margin: 0 auto;
            display: block;
            cursor: pointer;
        }
        
        .login {
            position: absolute;
            display: none;
            height: 300px;
            width: 400px;
            background-color: white;
            border-radius: 10px;
            box-shadow: 0px 2px 5px rgb(97, 97, 97);
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
        }
        
        .login .login-title {
            width: 234px;
            position: relative;
            left: 164px;
            font-size: 20px;
            line-height: 50px;
            font-weight: 500;
        }
        
        .login .login-title span {
            width: 35px;
            height: 35px;
            background-color: white;
            box-shadow: 0px 2px 5px rgb(126, 126, 126);
            display: block;
            border-radius: 50%;
            line-height: 35px;
            text-align: center;
            position: absolute;
            top: -14px;
            right: -11px;
            font-size: 12px;
        }
        
        .login .login-button {
            height: 30px;
            width: 100px;
            border: 1px solid black;
            line-height: 30px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="login-header">
        <a id="link" href="javascript:;">点击，弹出登录框</a>
    </div>
    <div id="login" class="login">
        <div id="title" class="login-title">登陆会员
            <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span>
        </div>
        <div class="login-input-content">
            <div class="login-input">
                <label>用户名</label>
                <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="">
            </div>
            <div class="login-input">
                <label>登陆密码</label>
                <input type="password" placeholder="请输入登录密码" name="info[username]" id="password" class="">
            </div>
        </div>
        <div id="loginBtn" class="login-button">
            <a href="javascript:void(0);" id="login-button-submit">登录会员</a>
        </div>
    </div>
    <!-- 遮盖层 -->
    <div id="bg" class="login-bg"></div>
    <script>
        // 开启关闭
        // 1.获取元素
        var login = document.querySelector('.login');
        var mask = document.querySelector('.login-bg');
        var link = document.querySelector('#link');
        var closeBtn = document.querySelector('#closeBtn');
        // 2.点击弹出层这个链接link让mask 和1ogin 显示出来
        link.addEventListener('click', function() {
            mask.style.display = 'block';
            login.style.display = 'block';
        })

        // 3.点击closeBtn 隐藏关闭窗口
        closeBtn.addEventListener('click', function() {
            mask.style.display = 'none';
            login.style.display = 'none';
        })

        // 拖拽效果
        // 1.获取元素
        var title = document.querySelector('#title');
        // 2.开始拖拽
        // (1)当我们鼠标按下，就获得鼠标在盒子内的坐标
        title.addEventListener('mousedown', function(e) {
            var x = e.pageX - login.offsetLeft;
            var y = e.pageY - login.offsetTop;
            // (2)鼠标移动的时候，把鼠标在页面中的坐标 减去鼠标在盒子内的坐标就是模态框的left和top值
            document.addEventListener('mousemove', move)

            function move(e) {
                login.style.left = e.pageX - x + 'px';
                login.style.top = e.pageY - y + 'px';
            }
            // (3)鼠标弹起就让鼠标移动事件移除
            document.addEventListener('mouseup', function() {
                document.removeEventListener('mousemove', move)
            })
        })
    </script>
</body>

</html>